package com.aduerr.v1shouyin.mvp.fragment;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.aduerr.v1shouyin.App;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.OrderGetPosOrderLEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.mvp.activity.BangGuidActivity;
import com.aduerr.v1shouyin.mvp.activity.LoginActivity;
import com.aduerr.v1shouyin.mvp.activity.OrderDetailActivity;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.internal.observers.BlockingBaseObserver;
import io.reactivex.schedulers.Schedulers;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.HashMap;

/**
 * 类名:
 * 此类用途: ---
 *
 * @Author: peixi
 * @Date: 2017-12-08 20:41
 * @Email: cr7inmanchester.com
 * @FileName:
 */
public class OrderNumFiltrateFregment extends Fragment {
    @BindView(R.id.tv_redound_money)
    EditText tvRedoundMoney;
    @BindView(R.id.tv_result)
    TextView tvResult;
    @BindView(R.id.tv_serch)
    TextView tvSerch;

    Unbinder unbinder;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.ordernumfiltrate_fregment, null, false);
        unbinder = ButterKnife.bind(this, inflate);
        return inflate;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_serch)
    public void onViewClicked() {

        String dingDanNumber = tvRedoundMoney.getText().toString().trim();
        if(TextUtils.isEmpty(dingDanNumber)) {
            ToastUtils.showToast(getActivity(), "订单号输入不能为空");
            return;
        } else {
            //请求网络
            requestNet(dingDanNumber);
        }

    }

    private void requestNet(String dingDanNumber) {
        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("page", 1 + "");
        addParams.put("orderid", dingDanNumber);
        APIRetrofit.getAPIService().orderGetPosOrderL(RXRequest.getXingPosParams(addParams, getActivity()))
                .subscribeOn(Schedulers.io())//开启一个io线程
                .observeOn(AndroidSchedulers.mainThread())//回到主线程
                .subscribe(new BlockingBaseObserver<OrderGetPosOrderLEntity>() {
                    @Override
                    public void onNext(OrderGetPosOrderLEntity orderGetPosOrderLEntity) {
                        //                    if (Tools.isAvailable
                        //                    (orderGetPosOrderLEntity)) return;
                        int success = orderGetPosOrderLEntity.getSuccess();
                        if(success == 1) {

                            if(orderGetPosOrderLEntity.getData().size() > 0) {
                                Intent intent = new Intent(getActivity(),
                                        OrderDetailActivity.class);
                                intent.putExtra("datebean",
                                        orderGetPosOrderLEntity.getData().get(0).getList().get(0));
                                startActivity(intent);
                            } else {
                                tvResult.setVisibility(View.VISIBLE);
                            }
                        } else if(success == -100 | success == -200) {
                            //// TODO: 2017/12/9 0009 绑定guid  界面
                            SharedPreferencesUtils.remove(getActivity(), "autologin");
                            SharedPreferencesUtils.remove(getActivity(),
                                    "remberpassword");
                            startActivity(new Intent(getActivity(),
                                    BangGuidActivity.class));
                            getActivity().finish();
                        } else if(success == -300 || success == -2 || success == -3 || success == -6) {
                            //// TODO: 2017/12/9 0009 去登录的界面
                            com.blankj.utilcode.util.ToastUtils.showShortSafe(orderGetPosOrderLEntity.getErrMsg());
                            SharedPreferencesUtils.remove(getActivity(), "autologin");
                            SharedPreferencesUtils.remove(getActivity(),
                                    "remberpassword");
                            startActivity(new Intent(getActivity(), LoginActivity.class));
                            getActivity().finish();
                        } else {
                            tvResult.setVisibility(View.VISIBLE);
                            ToastUtils.showToast(getActivity(),
                                    orderGetPosOrderLEntity.getErrMsg() + "");

                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(e instanceof SocketTimeoutException) {
                            Toast.makeText(App.getApp(), "网络连接超时", Toast.LENGTH_SHORT).show();
                        } else if(e instanceof ConnectException) {
                            Toast.makeText(App.getApp(), "连接失败", Toast.LENGTH_SHORT).show();
                        } else {
                            Log.e("onError:", "error:" + e.getMessage());//R.string
                            // .checknet +
                            Toast.makeText(App.getApp(), "检查网络状态", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

    }
}
